package com.example.zheng.util;

/**
 * @date 2021/11/16 - 19:34
 */
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;

@Data
@ApiModel(description = "返回类")
public class Result<T> {

//    @ApiModelProperty(value = "状态码")
//    private int status;
//
//    @ApiModelProperty(value = "错误信息")
//    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;

    @ApiModelProperty(value = "返回提示信息")
    private HashMap meta = new HashMap();

    public static final int SUCCESS_Status = 200;

    public static final int FAILURE_Status = 204;

    /**
     * 错误状态码
     */
    public static final int FAILURE_Status_201 = 201;

    /**
     * 错误状态码
     */
    public static final int FAILURE_Status_204 = 204;

    /**
     *
     */
    public static final int PARAERROR_Status_1004 = 1004;

    /**
     *
     */
    public static final int USERERROR_Status_1005 = 1005;

    public Result(){
        super();
    }

    public Result(int status, String msg, T data) {
        super();
        //this.status = status;
        //this.msg = msg;
        //System.out.println(status+"=="+msg);
        this.meta.put("status",status);
        this.meta.put("msg",msg);
        this.data = data;
    }

    public static Result success() {
        return new Result(SUCCESS_Status, "处理成功", null);
    }

    public static Result success(String msg) {
        return new Result(SUCCESS_Status, msg, null);
    }

    public static Result success(Object data) {
        System.out.println(data+"===");
        return new Result(SUCCESS_Status, "成功", data);
    }

    public static Result success(String msg, Object data) {
        return new Result(SUCCESS_Status, msg, data);
    }

    public static Result failure() {
        return new Result(FAILURE_Status, "失败", null);
    }

    public static Result failure(String msg) {
        return new Result(FAILURE_Status, msg, null);
    }

    public static Result failure(Object data) {
        return new Result(FAILURE_Status, "失败", data);
    }

    public static Result failure(String msg, Object data) {
        return new Result(FAILURE_Status, msg, data);
    }

    public static Result paraError() {
        return new Result(PARAERROR_Status_1004, "��������", null);
    }

    public static Result userError() {
        return new Result(USERERROR_Status_1005, "�û���Ϣ����", null);
    }

    public String toJSON() {
        return JSON.toJSONString(this);
    }
}

